package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.service.ISkuPropertyValueService;
import cn.wolfcode.shop.vo.GenerateSkuVo;
import cn.wolfcode.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productSku")
public class ProductSkuController {


    @Reference
    private IProductService productService;
    @Reference
    private ISkuPropertyService skuPropertyService;
    @Reference
    private ISkuPropertyValueService skuPropertyValueService;
    @Reference
    private IProductSkuService productSkuService;

    /**
     * 点击sku连接,显示页面
     *
     * @return
     */
    @RequestMapping("")
    public String generateSkuPage(Long productId, Model model) {

        Product product = productService.getProductById(productId);
        model.addAttribute("product", product);
        //判断是否已经生成sku数据了, 根据producId 查询数据,大于0即已经生成
        List<ProductSku> productSkuList = productSkuService.queryByProductId(product.getId());
        if (productSkuList.size() > 0) {
            //如果已经生成-----> 跳转显示页面
            List<SkuProperty> skuPropertyList = new ArrayList<SkuProperty>();

            for (ProductSkuProperty psp : productSkuList.get(0).getProductSkuPropertyList()) {
                skuPropertyList.add(psp.getSkuProperty());

            }

            model.addAttribute("skuPropertyList",skuPropertyList);
            model.addAttribute("skuList",productSkuList);
            return "product_sku/sku_list";
        }

        //如果没有生成----->


        model.addAttribute("skuPropertyList", skuPropertyService.queryByCId(product.getCatalog().getId()));
        return "product_sku/generate_sku";
    }

    /**
     * 点击商品的属性, 列出该属性的所有值
     *
     * @param skuPropertyId
     * @param model
     * @return
     */
    @RequestMapping("/getSkuPropertyValue")
    public String getSkuPropertyValuePage(Long skuPropertyId, Model model) {

        model.addAttribute("skuProperty", skuPropertyService.get(skuPropertyId));

        model.addAttribute("skuPropertyValueList", skuPropertyValueService.queryBySkuPropertyId(skuPropertyId));

        return "product_sku/sku_property_value_table";
    }


    /**
     * 生成sku数据显示
     * GenerateSkuVo 封装生成sku数据
     *
     * @param generateSkuVo
     * @return
     * @RequestBody 接收前台传过来的json数据
     */
    @RequestMapping("/generateSku")
    public String generateSku(@RequestBody GenerateSkuVo generateSkuVo, Model model) {

        List<Map> skuList = skuPropertyService.generateSku(generateSkuVo);

        model.addAttribute("skuPropertyList", generateSkuVo.getSkuPropertyList());
        model.addAttribute("skuList", skuList);

        return "product_sku/generateSkuList";
    }


    /**
     * 保存生成sku的数据
     *
     * @param generateSkuVo
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JSONResultVo save(GenerateSkuVo generateSkuVo) {

        productSkuService.saveSku(generateSkuVo);
        return JSONResultVo.instance();
    }

}
